package de.tum.in.protpred.eval;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import weka.classifiers.Evaluation;

public class TMHEvaluater
{
	public static void calcPerResidueScores(double [][]confMatrix, Evaluation eval,  String outputPath)
	{
		double tp = confMatrix[0][0];
		double fp = confMatrix[1][0];
		double fn = confMatrix[0][1];
		double tn = confMatrix[1][1];
		double qObs2T = tp / ( tp + fn);
		double qPred2T = tp / (tp + fp);
		double qObs2N = tn / (tn + fp);
		double qPred2N = tn / (tn + fn);
		double q2 = (tp + tn) / (tp + tn + fp + fn);
		double mcc = (( tp * tn ) - (fp * fn) ) /  Math.sqrt(((tp+fp)*(tp+fn)*(tn+fp)*(tn+fn))) ;
		double rmse  = eval.rootMeanSquaredError();
		
		BufferedWriter writer = null;
		
		try
		{
			File file = new File(outputPath);
			if (!file.exists()) {
				file.createNewFile();
			}
			writer = new BufferedWriter(new FileWriter(file));
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		try {
			writer.append("TP:" + tp + "\n");
			writer.append("TN: " + tn + "\n");
			writer.append("FP: " + fp + "\n");
			writer.append("FN: " + fn + "\n");	
			writer.append("Q^Obs_2T: " + qObs2T+ "\n");
			writer.append("Q^Pred_2T: " + qPred2T+ "\n");
			writer.append("Q^Obs_2N: " + qObs2N+ "\n");
			writer.append("Q^Pred_2N: " + qPred2N+ "\n");
			writer.append("Q2: " + q2 + "\n");
			writer.append("RMSE: " + rmse + "\n");
			writer.append("MCC: " + mcc+ "\n");
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}

}
